//
//  UIView+callingAlertview.m
//  IAmMatchmaker
//
//  Created by Herui on 15/5/15.
//  Copyright (c) 2015年 herui. All rights reserved.
//

#import "UIView+callingAlertview.h"
#import "NSString+RealPhoneNumber.h"

@implementation UIView (callingAlertview)

+ (UIView *)callAphoneNumber:(NSString *)phoneNumber withTitle:(NSString *)title
{
    if (!phoneNumber) {
        return nil;
    }
    if (SYSTEM_IS_IOS8) {
        // ios8开始拨打电话可以返回
        NSString *newTitle;
        if (title) {
            newTitle = [NSString stringWithFormat:@"%@：%@？", title, phoneNumber];
        } else {
            newTitle = [NSString stringWithFormat:@"%@：%@？", NSLocalizedString(@"您确定要拨打电话", nil), phoneNumber];
        }
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil) message:newTitle delegate:nil cancelButtonTitle:NSLocalizedString(@"取消", @"") otherButtonTitles:NSLocalizedString(@"确定", @""), nil];
        return alertView;
    } else {
        // ios7拨打电话不可以返回
        phoneNumber = [NSString getRealNumber:phoneNumber];
        UIWebView *callWebview =[[UIWebView alloc] initWithFrame:CGRectZero];
        NSURL *telURL =[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        
        return callWebview;
    }
}

- (void)callANumber:(NSString *)number
{
    number = [NSString getRealNumber:number];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", number]];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
        
    }
}


@end
